<?php

defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Admin controller for the users module
 *
 * @author 		Phil Sturgeon - PyroCMS Dev Team
 * @package 	PyroCMS
 * @subpackage 	Users module
 * @category	Modules
 */
class Admin extends Admin_Controller {

    /**
     * Constructor method
     * @access public
     * @return void
     */
    public function __construct() {
        // Call the parent's constructor method
        parent::__construct();
        $this->load->model('Categories_m');
        $this->load->library('categories_lib');
    }

    /**
     * List all users
     * @access public
     * @return void
     */
    public function index() {
        $this->listing();
    }

    function listing() {
        $this->data['module_title'] = 'Liệt kê danh mục sản phẩm';
        $this->data['cats'] = $this->Categories_m->get_product_categories();
        $this->template
                ->set_hook('TOOLBAR', 'cat_toolbar')
                ->build('listing', $this->data);
        //$this->load->view('test_form', $data);
    }

    function add($id_parent=1) {
        $this->data['module_title'] = 'Thêm danh mục';
        $this->data['id_parent'] = $id_parent;
        $this->categories_lib->build_tree_option(1, 'Danh muc goc', 1);
        $this->data['cate_options'] = $this->categories_lib->option_store;
        $this->template
                ->set_hook('TOOLBAR', 'cat_toolbar')
                ->build('add', $this->data);
    }

    function update($id_category=FALSE) {
        if (!$id_category) {
            redirect(site_url() . 'admin/categories/listing');
        }
        $vi_category= $this->Categories_m->get_category_details($id_category,1);
        $fr_category= $this->Categories_m->get_category_details($id_category,2);
        if(!$vi_category||!$fr_category){
            redirect(site_url() . 'admin/categories/listing');
        }
        $this->data['vi_category'] = $vi_category;
        $this->data['fr_category'] = $fr_category;
        $this->data['module_title'] = 'Thêm danh mục';
        $this->data['id_category'] = $id_category;
        $this->categories_lib->build_tree_option(1, 'Danh mục', 1);
        $this->data['cate_options'] = $this->categories_lib->option_store;
        $this->template
                ->set_hook('TOOLBAR', 'cat_toolbar')
                ->build('update', $this->data);
    }
    function ajax_update_category(){
        if ($this->Categories_m->update_category($_POST) > 0) {
            echo 1;
        } else {
            echo 0;
        }
    }
    function json_combo() {
        $this->categories_lib->build_tree_option(1, 'Danh muc goc', 1);
        echo '{identifier:"id",label:"name",items:' . json_encode($this->categories_lib->option_store) . '}';
    }

    function json_category_group() {
        $data = $this->Categories_m->get_categories_group();
        echo '{identifier:"id",label:"name",items:' . json_encode($data) . '}';
    }

    function json_data() {
        $cat_tree = $this->categories_lib->build_gicms_category_tree(1);
        print_r($cat_tree);
    }

    function rebuild_tree($cat_tree) {
        $items = array();
        $cat = $cat_tree;
        $items = array(
            array(
                'id' => $cat['id'], 'name' => $cat['name'], 'numberOfItems' => count($cat['children']), 'class' => 'flatScreenIcon', 'children' => $this->remane_children_array($cat['children'])
            )
        );
        return array('identifier' => 'id', 'label' => 'name', 'items' => $items);
    }

    function remane_children_array($childrens) {
        $childs = array();
        //print_r($childrens);die;
        for ($i = 0; $i < count($childrens); $i++) {
            $children = $childrens[$i];
            if (array_key_exists('children', $children)) {
                $childs[$i] = array('id' => $children['id'], 'name' => $children['name'], 'numberOfItems' => count($children['children']), 'class' => 'flatScreenIcon', 'children' => $this->remane_children_array($children['children']));
            } else {
                $childs[$i] = array('id' => $children['id'], 'name' => $children['name'], 'numberOfItems' => 0);
            }
        }
        return $childs;
    }

    function delete_categories($id) {
        $this->categories_lib->delete_child_categories($id);
        $this->categories_lib->delete_category($id);
    }

    function add_parent_categories() {
        print_r($_POST);
        if ($this->Categories_m->add_parent_cat($_POST) > 0) {
            echo 1;
        } else {
            echo 0;
        }
    }

    function add_sub_categories() {
        print_r($_POST);
    }

}